$(function(){
	new WOW().init();
	var baseUrl = 'https://klingyang.com/api2/yqft/';
	// 测试页面状态
	var type = 1;
	var canClick = true;
	var timeout;
	var times = 60;
	
	if(getQueryVariable('type')){
		type = getQueryVariable('type');
		setPage();
	}
	
	
	function setPage() {
		if(type == 1){
			$('.page1').fadeIn(0);
			$('.page2').fadeOut(0);
			$('.page3').fadeOut(0);
		} else if(type == 2){
			$('.page1').fadeOut(0);
			$('.page2').fadeIn(0);
			$('.page3').fadeOut(0);
		} else if( type == 3){
			$('.page1').fadeOut(0);
			$('.page2').fadeOut(0);
			$('.page3').fadeIn(0);
		}
	}
	
	
	
	
	$('.sms_btn').click(function(){
		var phone = $('.tel').val();
		if(phone == '' || phone == undefined || phone == null){
			alert('请输入电话');
			return false;
		}
		if(phone.length != 11){
			alert('手机号格式不正确');
			return false;
		}
		if(canClick){
			canClick = false;
			$.ajax({
				url:baseUrl + 'sendMsg',
				method:'post',
				dataType:'json',
				data:{
					phone:phone
				},
				success:function(res){
					if(res.status == 1){
						clearInterval(timeout);
						timeout = setInterval(function(){
							times--;
							if(times <= 0){
								canClick = true;
								times = 60;
								$('.sms_btn').text('获取验证码');
							}else{
								$('.sms_btn').text(times);
							}
						},1000);
						alert('发送成功');
					}else{
						alert(res.msg);
						canClick = true;
					}
				}
			})
			
			
		}
	});

	
	
	getProvince();
	
	function getProvince(){
		$.ajax({
			url: baseUrl + 'getProvince',
			method:'get',
			dataType:'json',
			data:{},
			success:function(res){
				console.log(res)
				if(res.status == 1){
					var data = res.data;
					$('.province_item').remove();
					for(var i = 0;i<data.length;i++){
						var html = '<option value="'+data[i]+'" class="province_item">'+data[i]+'</option>';
						$('.province').append(html);
					}
				}
			}
		})
	}
	$('.province').on('change',function(){
		var pro = $('.province').val();
		console.log(pro)
		if(pro != -1){
			getCity(pro);
		} else {
			$('.city_item').remove();
			$('.store_item').remove();
		}
	});
	
	function getCity(pro){
		$.ajax({
			url: baseUrl + 'getCityByProvince',
			method:'get',
			dataType:'json',
			data:{
				province: pro
			},
			success:function(res){
				console.log(res)
				if(res.status == 1){
					var data = res.data;
					$('.city_item').remove();
					for(var i =0 ;i<data.length;i++){
						var html = '<option value="'+data[i]+'" class="city_item">'+data[i]+'</option>';
						$('.city').append(html);
					}
				}
			}
		})
	}
	$('.city').on('change',function(){
		var cit = $('.city').val();
		console.log(cit);
		if(cit != -1){
			getStore(cit);
		}else{
			$('.store_item').remove();
		}
	});
	
	function getStore(city){
		$.ajax({
			url: baseUrl + 'getStoreByCity',
			method:'get',
			dataType:'json',
			data:{
				city:city
			},
			success:function(res){
				console.log(res);
				if(res.status == 1){
					var data = res.data;
					$('.store_item').remove();
					for(var i = 0;i<data.length;i++){
						var html = '<option value="'+data[i]+'" class="store_item">'+data[i]+'</option>';
						$('.store').append(html);
					}
				}
			}
		})
	}
	
	$('.button').click(function(){
		var name = $('.name').val();
		var tel = $('.tel').val();
		var address = $('.address').val();
		var province = $('.province').val();
		var city = $('.city').val();
		var store = $('.store').val();
		if(name == '' || name == undefined || name == null){
			alert('请输入姓名');
			return false;
		}
		if(tel == '' || tel == undefined || tel == null){
			alert('请输入电话');
			return false;
		}
		if(tel.length != 11){
			alert('手机号格式不正确');
			return false;
		}
		if(address == '' || address == undefined || address == null){
			alert('请输入验证码');
			return false;
		}
		if(province == '' || province == undefined || province == null || province == -1){
			alert('请选择省份');
			return false;
		}
		if(city == '' || city == undefined || city == null || city == -1){
			alert('请选择城市');
			return false;
		}
		if(store == '' || store == undefined || store == null || store == -1){
			alert('请选择经销商');
			return false;
		}
		$.ajax({
			url: baseUrl + 'saveMsg',
			method:'post',
			dataType:'json',
			data:{
				phone:tel, //电话 
				name:name,    //姓名
				code:address,    //验证码
				province :province,   //省份
				city  :city,           //城市
				store  :store,         //经销商
			},
			success:function(res){
				if(res.status == 1){
					$('.page1').fadeOut(0);
					$('.page2').fadeIn(0);
				} else{
					alert(res.msg);
				}
			},
			error:function(res){
				console.log(res)
			}
		})
		
	})
	
	
	
	
	
	function getQueryVariable(variable){
	   var query = window.location.search.substring(1);
	   var vars = query.split("&");
	   for (var i=0;i<vars.length;i++) {
			   var pair = vars[i].split("=");
			   if(pair[0] == variable){return pair[1];}
	   }
	   return(false);
	}
	
	window.alert = function(name){
		var iframe = document.createElement("IFRAME");
		iframe.style.display="none";
		iframe.setAttribute("src", 'data:text/plain,');
		document.documentElement.appendChild(iframe);
		window.frames[0].window.alert(name);
		iframe.parentNode.removeChild(iframe);
	}
})